package com.ruoyi.wmsorder.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.wmsorder.domain.WmsShipmentOrderDetail;
import com.ruoyi.wmsorder.mapper.WmsShipmentOrderMapper;
import com.ruoyi.wmsorder.domain.WmsShipmentOrder;
import com.ruoyi.wmsorder.service.IWmsShipmentOrderService;

/**
 * 销售发货出库Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-03-20
 */
@Service
public class WmsShipmentOrderServiceImpl implements IWmsShipmentOrderService 
{
    @Autowired
    private WmsShipmentOrderMapper wmsShipmentOrderMapper;

    /**
     * 查询销售发货出库
     * 
     * @param id 销售发货出库主键
     * @return 销售发货出库
     */
    @Override
    public WmsShipmentOrder selectWmsShipmentOrderById(Long id)
    {
        return wmsShipmentOrderMapper.selectWmsShipmentOrderById(id);
    }

    /**
     * 查询销售发货出库列表
     * 
     * @param wmsShipmentOrder 销售发货出库
     * @return 销售发货出库
     */
    @Override
    public List<WmsShipmentOrder> selectWmsShipmentOrderList(WmsShipmentOrder wmsShipmentOrder)
    {
        return wmsShipmentOrderMapper.selectWmsShipmentOrderList(wmsShipmentOrder);
    }

    /**
     * 新增销售发货出库
     * 
     * @param wmsShipmentOrder 销售发货出库
     * @return 结果
     */
    @Transactional
    @Override
    public int insertWmsShipmentOrder(WmsShipmentOrder wmsShipmentOrder)
    {
        wmsShipmentOrder.setCreateTime(DateUtils.getNowDate());
        int rows = wmsShipmentOrderMapper.insertWmsShipmentOrder(wmsShipmentOrder);
        insertWmsShipmentOrderDetail(wmsShipmentOrder);
        return rows;
    }

    /**
     * 修改销售发货出库
     * 
     * @param wmsShipmentOrder 销售发货出库
     * @return 结果
     */
    @Transactional
    @Override
    public int updateWmsShipmentOrder(WmsShipmentOrder wmsShipmentOrder)
    {
        wmsShipmentOrder.setUpdateTime(DateUtils.getNowDate());
        wmsShipmentOrderMapper.deleteWmsShipmentOrderDetailByShipmentOrderId(wmsShipmentOrder.getId());
        insertWmsShipmentOrderDetail(wmsShipmentOrder);
        return wmsShipmentOrderMapper.updateWmsShipmentOrder(wmsShipmentOrder);
    }

    /**
     * 批量删除销售发货出库
     * 
     * @param ids 需要删除的销售发货出库主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWmsShipmentOrderByIds(Long[] ids)
    {
        wmsShipmentOrderMapper.deleteWmsShipmentOrderDetailByShipmentOrderIds(ids);
        return wmsShipmentOrderMapper.deleteWmsShipmentOrderByIds(ids);
    }

    /**
     * 删除销售发货出库信息
     * 
     * @param id 销售发货出库主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteWmsShipmentOrderById(Long id)
    {
        wmsShipmentOrderMapper.deleteWmsShipmentOrderDetailByShipmentOrderId(id);
        return wmsShipmentOrderMapper.deleteWmsShipmentOrderById(id);
    }

    /**
     * 新增出库单详情信息
     * 
     * @param wmsShipmentOrder 销售发货出库对象
     */
    public void insertWmsShipmentOrderDetail(WmsShipmentOrder wmsShipmentOrder)
    {
        List<WmsShipmentOrderDetail> wmsShipmentOrderDetailList = wmsShipmentOrder.getWmsShipmentOrderDetailList();
        Long id = wmsShipmentOrder.getId();
        if (StringUtils.isNotNull(wmsShipmentOrderDetailList))
        {
            List<WmsShipmentOrderDetail> list = new ArrayList<WmsShipmentOrderDetail>();
            for (WmsShipmentOrderDetail wmsShipmentOrderDetail : wmsShipmentOrderDetailList)
            {
                wmsShipmentOrderDetail.setShipmentOrderId(id);
                list.add(wmsShipmentOrderDetail);
            }
            if (list.size() > 0)
            {
                wmsShipmentOrderMapper.batchWmsShipmentOrderDetail(list);
            }
        }
    }
}
